﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VideoMonitorSystem.Model;
using System.Data;
using System.Data.SqlClient;

namespace VideoMonitorSystem.DAL
{   
    /// <summary>
    /// Flash文件数据层处理类
    /// </summary>
    public class SwfFileService
    {

        private static SwfFile GetSwfFileByDataRow(DataRow row)
        {
            SwfFile swf = new SwfFile();
            swf.FID = Convert.ToInt32(row["fID"].ToString());
            swf.FName = (string)row["fName"].ToString();
            swf.FDesc = (string)row["fDesc"].ToString();
            swf.FPath = (string)row["fPath"].ToString();
            swf.FInTime = Convert.ToDateTime(row["fInTime"].ToString());
            swf.DeptInfo = (DeptInfo)DeptInfoService.GetDeptInfoByDID(Convert.ToInt32(row["sID"].ToString()));
            return swf;
        }


        private static List<SwfFile> GetSwfFileByDataTable(DataTable dataTable)
        {
            List<SwfFile> list = new List<SwfFile>();
            foreach (DataRow row in dataTable.Rows)
            {
                list.Add(GetSwfFileByDataRow(row));
            }
            return list;
        }

        private static List<SwfFile> GetAllSwfFileBySQL(string sql)
        {
            return GetSwfFileByDataTable(DBHelper.GetDataTable(sql));
        }
        
        /// <summary>
        /// 依据Flash文件ID获得文件信息
        /// </summary>
        /// <param name="fID">文件ID</param>
        /// <returns>Flash文件对象</returns>
        public static SwfFile GetSwfFileByFID(int fID)
        {
            string sql = "SELECT * FROM swfFiles WHERE fID = @fID";
            DataRow row = DBHelper.GetDataTable(sql, new SqlParameter("@fID", fID)).Rows[0];
            return GetSwfFileByDataRow(row);
        }
        /// <summary>
        /// 获取所有的Flash文件
        /// </summary>
        /// <returns>Flsah文件集合</returns>
        public static List<SwfFile> GetAllSwfFiles()
        {
            string sql = "SELECT * FROM swfFiles";
            return GetAllSwfFileBySQL(sql);
        }
        /// <summary>
        /// 依据部门类型查询Flash文件
        /// </summary>
        /// <param name="dID">部门ID</param>
        /// <returns>所属部门的Flash文件的集合</returns>
        public static List<SwfFile> GetAllSwfFileByDID(int dID)
        {
            string sql = "SELECT * FROM swfFiles WHERE dID = @dID";
            List<SwfFile> list = new List<SwfFile>();

            using (DataTable dataTable = DBHelper.GetDataTable(sql, new SqlParameter("@dID", dID)))
            {
                list = GetSwfFileByDataTable(dataTable);
            }
            return list;
        }

        /// <summary>
        /// 新增一个Flash文件对象
        /// </summary>
        /// <param name="swfFile">Flash对象</param>
        /// <returns>受影响行数</returns>
        public static int AddSwfFile(SwfFile swfFile)
        {
            string sql = "INSERT INTO swfFiles(fName,fDesc,fPath) values(@fName,@fDesc,@fPath)";
            SqlParameter[] par = new SqlParameter[]
            {
                new SqlParameter("@fName",swfFile.FName),
                new SqlParameter("@fDesc",swfFile.FDesc),
                new SqlParameter("@fPath",swfFile.FPath)
            };
            return DBHelper.GetScalar(sql,par);
        }

         /// <summary>
         /// 删除指定的Flash文件
         /// </summary>
         /// <param name="fID">Flash文件ID</param>
         /// <returns>受影响的行数</returns>
        public static int DelSwfFileByFID(int fID)
        {
            string sql = "DELETE FROM swfFiles WHERE fID = @fID";
            return DBHelper.GetScalar(sql,new SqlParameter("@fID",fID));
        }
        /// <summary>
        /// 批量删除Flash文件
        /// </summary>
        /// <param name="fIDs">Flash文件ID数组</param>
        /// <returns>被删除的数量</returns>
        public static int DelSwfFileByArrayFID(string[] fIDs)
        {
            int reslut = 0;
            foreach(string fID in fIDs)
            {
                DelSwfFileByFID(Convert.ToInt32(fID));
                reslut++;
            }
            return reslut;
        }
        /// <summary>
        /// 删除指定部门的Flash文件
        /// </summary>
        /// <param name="dID">部门ID</param>
        /// <returns>被删除的行数</returns>
        public static int DelSwfFileByDID(int dID)
        {
            string sql = "DELETE FROM swfFiles WHERE dID = @dID";
            return DBHelper.GetScalar(sql,new SqlParameter("@dID",dID));
        }

    }
}
